{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【项目02】  基于Python的算法函数创建\n",
    "\n",
    "作业要求：\n",
    "根据不同题目，完成代码书写并成功运行\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "满足条件的数字为：12\n",
      "满足条件的数字为：13\n",
      "满足条件的数字为：14\n",
      "满足条件的数字为：21\n",
      "满足条件的数字为：23\n",
      "满足条件的数字为：24\n",
      "满足条件的数字为：31\n",
      "满足条件的数字为：32\n",
      "满足条件的数字为：34\n",
      "满足条件的数字为：41\n",
      "满足条件的数字为：42\n",
      "满足条件的数字为：43\n",
      "总共有12个结果\n"
     ]
    }
   ],
   "source": [
    "# 题目1：有1、2、3、4个数字，能组成多少个互不相同且无重复数字的两位数？都是多少？\n",
    "# 该题目不用创建函数\n",
    "\n",
    "n = 0  # 计数变量 \n",
    "for i in range(1,5):\n",
    "    for j in range(1,5):\n",
    "        if i != j:\n",
    "            n += 1\n",
    "            print(\"满足条件的数字为：%i%i\" %(i,j))\n",
    "print('总共有%i个结果' %n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第1个数字：50\n",
      "请输入第2个数字：a\n",
      "输入内容不为数字，请重新输入第2个数字：60\n",
      "请输入第3个数字：30\n",
      "请输入第4个数字：20\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[20.0, 30.0, 50.0, 60.0]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 题目2：输入三个整数x,y,z，请把这三个数由小到大输出，可调用input()。（需要加判断：判断输入数据是否为数字）\n",
    "# 提示：判断是否为数字：.isdigit()\n",
    "# 该题目需要创建函数\n",
    "\n",
    "def f(n):\n",
    "    lst =[]\n",
    "    for i in range(1,n+1):\n",
    "        x = input('请输入第%i个数字：'%i)\n",
    "        while x.isdigit() == False:\n",
    "            x = input('输入内容不为数字，请重新输入第%i个数字：'%i)\n",
    "        else:\n",
    "            lst.append(float(x))\n",
    "    return(sorted(lst))\n",
    "\n",
    "f(4)  # 执行函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一行字符：\n",
      "hello world! 123,'\n",
      "char = 10,space = 2,digit = 3,others = 3\n"
     ]
    }
   ],
   "source": [
    "# 题目3：输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。\n",
    "# 提示：利用while语句,条件为输入的字符不为'\\n'.\n",
    "# 该题目不需要创建函数\n",
    "\n",
    "st = input('请输入一行字符：\\n')\n",
    "letters = 0\n",
    "space = 0\n",
    "digit = 0\n",
    "others = 0\n",
    "for c in st:\n",
    "    if c.isalpha():\n",
    "        letters += 1\n",
    "    elif c.isspace():\n",
    "        space += 1\n",
    "    elif c.isdigit():\n",
    "        digit += 1\n",
    "    else:\n",
    "        others += 1\n",
    "print ('char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一天猴子共摘了1534个\n"
     ]
    }
   ],
   "source": [
    "# 题目4：猴子吃桃问题\n",
    "# 猴子第一天摘下若干个桃子，当即吃了一半，还不瘾，又多吃了一个,\n",
    "# 第二天早上又将剩下的桃子吃掉一半，又多吃了一个。\n",
    "# 以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时，见只剩下一个桃子了。求第一天共摘了多少?\n",
    "# 提示：采取逆向思维的方法，从后往前推断。\n",
    "# 该题目不需要创建函数\n",
    "\n",
    "n = 1\n",
    "for day in range(9,0,-1):\n",
    "    m = (n + 1) * 2\n",
    "    n = m\n",
    "print('第一天猴子共摘了%i个' %n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "do you want to play it.('y' or 'n')n\n",
      "hehe\n"
     ]
    }
   ],
   "source": [
    "# 题目5：猜数字问题，要求如下：\n",
    "# ① 随机生成一个整数\n",
    "# ② 猜一个数字并输入\n",
    "# ③ 判断是大是小，直到猜正确\n",
    "# ④ 判断时间\n",
    "# 提示：需要用time模块、random模块\n",
    "# 该题目不需要创建函数\n",
    "\n",
    "import time\n",
    "import random\n",
    "    \n",
    "play_it = input('do you want to play it.(\\'y\\' or \\'n\\')')   # 询问是否参与游戏\n",
    "while play_it == 'y':\n",
    "    c = input('input a character:\\n')   # 输入参与游戏人物\n",
    "    i = random.randint(0,100)\n",
    "    print( 'please input number you guess:\\n')\n",
    "    a = time.time()  # 记录开始时间\n",
    "    guess = int(input('input your guess:\\n'))\n",
    "    while guess != i:\n",
    "        if guess > i:\n",
    "            print( 'please input a little smaller')\n",
    "            guess = int(input('input your guess:\\n'))\n",
    "        else:\n",
    "            print( 'please input a little bigger')\n",
    "            guess = int(input('input your guess:\\n'))\n",
    "    b = time.time()  # 记录结束时间\n",
    "    usedtime = b - a\n",
    "    print( 'It took you %.2f seconds' % usedtime)\n",
    "    if usedtime < 15:\n",
    "        print( 'you are very clever!')\n",
    "    elif usedtime < 25:\n",
    "        print( 'you are normal!')\n",
    "    else:\n",
    "        print( 'you are stupid!')\n",
    "    print( 'Congradulations')\n",
    "    print( 'The number you guess is %d' % i)\n",
    "    break\n",
    "else:\n",
    "    print('hehe')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
